<?php
namespace App\Service;
use App\Common\Utill\OperationLog;
use App\Models\Admin;
use App\Models\WebsitesAdmin;
use App\Models\WebsiteStyle;
use Illuminate\Support\Facades\DB;

class WebsitestyleService extends CommonService{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 列表数据
     * @param $request
     * @param $num
     * @return array|mixed
     */
    public function getListData($request, $num)
    {
        $model = new WebsiteStyle();
        $query = $model::query();
        $query->where('is_del',1);
        //标题
        $title = $request->input('title', '');
        if ($title) {
            $query->where('names', 'like', '%' . $title . '%');
        }
        //状态
        $status = $request->input('status', '');
        if ($status) {
            $query->where('status', $status);
        }
        $query->orderBy('id', 'desc');
        $result = $query->paginate($num)->toArray();
        foreach ($result['data'] as $key => $val) {
            $result['data'][$key]['statusName'] = $val['status'] == 1 ? '开启' : '关闭';
        }
        return $this->success('SUCCESS', $result['total'], $result['data']);
    }

    /**
     * 添加保存
     * @param $names
     * @param $domains
     * @return array|mixed
     */
    public function store($names,$content)
    {
        $isConfirm = WebsiteStyle::query()->where("names",$names)->exists();
        if($isConfirm){
            return $this->error('网站风格不能为空');
        }
        $postData['names'] = $names;
        $postData['content'] = $content;
        $postData['status'] = 1;
        $postData['is_del'] = 1;
        $postData['created_at'] = date('Y-m-d H:i:s');
        $lastId = WebsiteStyle::query()->insertGetId($postData);
        if(!$lastId){
            return $this->error('添加失败');
        }
        OperationLog::record(1,'网站风格-添加',$lastId,$postData);
        return $this->success('添加成功');
    }

    /**
     * 编辑保存
     * @param $title
     * @param $domains
     * @return array|mixed
     */
    public function update($id,$names,$content)
    {
        $isConfirm = WebsiteStyle::query()->where('id','<>',$id)->where("names",$names)->exists();
        if($isConfirm){
            return $this->error('网站风格已存在');
        }
        $oldData = WebsiteStyle::query()->where('id',$id)->first()->toArray();
        $postData['names'] = $names;
        $postData['content'] = $content;
        $postData['updated_at'] = date('Y-m-d H:i:s');
        $result = WebsiteStyle::query()->where('id', $id)->update($postData);
        if(!$result){
            return $this->error('编辑失败');
        }
        OperationLog::record(2,'网站风格-编辑',$id,$postData,$oldData);
        return $this->success('编辑成功');
    }

    /**
     * 状态修改
     * @return void
     */
    public function status($id)
    {
        $isShow = Websites::query()->where(['id'=>$id])->value('status');
        $data['status'] = $isShow == 1 ? -1 : 1;
        $data['updated_at'] = date('Y-m-d H:i:s');
        $result = Websites::query()->where('id', $id)->update($data);
        if (!$result) {
           return $this->error('设置失败');
        }
        $this->articleStatus($id,$data['status']);
        OperationLog::record(2,'网站管理-状态编辑',$id,['status'=>$data['status']],['status'=>$isShow]);
        return $this->success('设置成功');
    }

    /**
     * 单个删除
     */
    public function delOne($id)
    {
        //是否有网站
        $isConfirm = WebsitesAdmin::query()->where("websites_style_id",$id)->exists();
        if($isConfirm){
            return $this->error('该风格下面有网站，不能删除');
        }
        $result = WebsiteStyle::findOrFail($id);
        $result->is_del = -1;
        $result->updated_at = date("Y-m-d H:i:s");
        if (!$result->save()) {
            return $this->error('删除失败');
        }
        //
        OperationLog::record(3,'网站风格-删除',$id);
        return $this->success("删除成功");
    }



    /**
     * 网站下面的文章操作
     * @param $id
     * @param $status
     * @return void
     */
    public function articleStatus($id,$status)
    {
        if($status == -1){//下架
            $whereIni['website_id'] = $id;
            $whereIni['status'] = 2;
            $countNumber = Articles::query()->where($whereIni)->count();
            if($countNumber){
                $saveData['status'] = -1;
                $saveData['from_source'] = 3;
                Articles::query()->where($whereIni)->update($saveData);
            }
        }else{//上架
            $onlineIni['website_id'] = $id;
            $onlineIni['from_source'] = 3;
            $countNumber = Articles::query()->where($onlineIni)->count();
            if($countNumber){
                $saveData['status'] = 2;
                $saveData['from_source'] = 4;
                Articles::query()->where($onlineIni)->update($saveData);
            }
        }
    }

    /**
     * 获取网站列表
     * @param $status  //状态 1=开启 2=全部
     * @param $arrType //1=一维数组 2=二维数组
     * @return mixed[]
     */
    public static function getWebsiteData($status = 2,$arrType=2)
    {
        $whereIni['is_del'] = 1;
        if($status == 1){
            $whereIni['status'] = 1;
        }
        $query = Websites::query();
        $accountAuthType = self::accountAuthType();
        if($accountAuthType != 1){
             $admindId = self::masterAccountId();
             $websiteAdmin = WebsitesAdmin::query()->where('admin_id',$admindId)->pluck('website_id')->toArray();
             if(!$websiteAdmin){
                   $query->where('id',0);
             }else{
                   $query->whereIn('id',$websiteAdmin);
             }
        }
        $data = $query->where($whereIni)
            ->select('id','names','domains')
            ->orderBy('id','desc')
            ->get()->toArray();
        if($arrType==1){
            $result =[];
            foreach ($data as $k => $v) {
                $result[$v['id']] = $v['names'];
            }
            return $result;
        }
        return $data;
    }
}
